#!/bin/bash
#
# /etc/init.d/jmxtrans -- startup script for jmxtrans
#
### BEGIN INIT INFO
# Provides:     jmxtrans
# Required-Start:  $time $network $remote_fs $syslog
# Required-Stop:   $time $network $remote_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    jmxtrans
### END INIT INFO

. /lib/lsb/init-functions

PIDFILE="/var/run/jmxtrans.pid"

# Run jmxtrans as this user ID and group ID
JMXTRANS_USER=jmxtrans
JMXTRANS_GROUP=jmxtrans

RUNCMD="/usr/share/jmxtrans/bin/jmxtrans.sh"

# overwrite settings from default file
if [ -f /etc/default/jmxtrans ]; then
    set -a
    source /etc/default/jmxtrans
    set +a
fi

RETVAL=0

do_start() {
    /usr/bin/touch "$PIDFILE" && /bin/chown "$JMXTRANS_USER":"$JMXTRANS_GROUP" "$PIDFILE"
    start-stop-daemon --start -b --pidfile $PIDFILE --oknodo \
        --chuid $JMXTRANS_USER:$JMXTRANS_GROUP -u $JMXTRANS_USER -g $JMXTRANS_GROUP -v \
        -d /usr/share/jmxtrans --exec $RUNCMD -- start
    RETVAL=$?
}

do_stop() {
    su -m jmxtrans -c "$RUNCMD stop"
    RETVAL=$?
}

do_status() {
    su -m jmxtrans -c "$RUNCMD status"
    RETVAL=$?
}

case "$1" in
  start)
    log_daemon_msg "Starting jmxtrans" "jmxtrans"
    do_start
    log_end_msg $RETVAL
    ;;
  stop)
    log_daemon_msg "Stopping jmxtrans" "jmxtrans"
    do_stop
    log_end_msg $RETVAL
    ;;

  restart|reload|force-reload|try-restart)
    log_daemon_msg "Restarting jmxtrans" "jmxtrans"
    do_stop
    do_start
    log_end_msg $RETVAL
    ;;

  status)
    log_daemon_msg "Status jmxtrans" "jmxtrans"
    do_status
    log_end_msg $RETVAL
    ;;

  *)
    log_action_msg "Usage: /etc/init.d/jmxtrans {start|stop|reload|force-reload|restart|try-restart|status}"
    exit 1
esac

exit $RETVAL
